<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>游戏包{{detail.id?'编辑':'添加'}}</legend>
        </fieldset>
        <form class="layui-form" action="javascript:;">

            <div class="layui-form-item">
                <label class="layui-form-label">选择渠道</label>
                <div class="layui-input-block">
                    <select v-model="detail.game_channel_id" class="layui-select" lay-filter="game_channel_id" lay-verify="required" >
                        <option value="">--请选择--</option>
                        <option v-for="item in channels" :value="item.id" lay-ignore>{{item.name}}</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">包号</label>
                <div class="layui-input-block">
                    <input type="text" v-model="detail.package_no" lay-verify="title" autocomplete="off" placeholder="请输入" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">渠道标识符</label>
                <div class="layui-input-block">
                    <input type="text" v-model="detail.channel" lay-verify="title" autocomplete="off" placeholder="请输入" class="layui-input" >
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label">渠道配置标识</label>
                <div class="layui-input-block">
                    <input type="text" v-model="detail.channelcg" lay-verify="title" autocomplete="off" placeholder="请输入" class="layui-input" readonly>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">商品配置</label>
                <div class="layui-input-block">
                    <select v-model="detail.configtype" class="layui-select" lay-filter="configtype" lay-verify="required">
                        <option value="">--请选择--</option>
                        <option v-for="item in configgroup" :value="item.type" lay-ignore>{{item.name}}</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">端版本</label>
                <div class="layui-input-block">
                    <select v-model="detail.type" class="layui-select" lay-filter="type" lay-verify="required">
                        <option value="">--请选择--</option>
                        <option v-for="item in types" :value="item.type" lay-ignore>{{item.name}}</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">包版本说明</label>
                <div class="layui-input-block">
                    <input type="text" v-model="detail.description" autocomplete="off" placeholder="请输入" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">测试包</label>
                <div class="layui-input-block">
                    <input type="radio" name="is_test" lay-filter="is_test" value="0" title="否" :checked="!Id || detail.is_test===0">
                    <input type="radio" name="is_test" lay-filter="is_test" value="1" title="是" :checked="detail.is_test===1">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script>
    from = null;
    Id = urlParam('id')
    var configgroup = getEnum('configgroup')

    new Vue({
        el:"#app",
        data:{
            types:[{type:1,name:"安卓"},{type:2,name:"IOS"},{type:3,name:"PC"}],
            channels:[],
            detail:{
                id:null,
                game_channel_id:null,
                package_no:null,
                type:null,
                description:null,
                channel:"",
                is_test:0,
                configtype:"",
                channelcg:''

            }
        },
        methods:{

        },
    
        updated:function(){
          form.render();
        },
        mounted:function(){
            var _this = this;

            layui.use(['form','jquery'], function () {
                form = layui.form;
                var layer = layui.layer ,$ = layui.jquery;

                //渠道列表
                $.get("/admin/gameChannel/paginate",{page:1,limit:9999},function(res){
                    _this.channels = res.data;
                });

                    if (Id) { // 编辑操作
            $.get('/admin/gamePackage/' + Id, {}, function(res) {
                console.log('响应数据:', res); // 调试信息
                _this.detail = res.data;

                // 在这里直接设置 channelcg
                const selectedChannel = _this.channels.find(item => item.id === Number(_this.detail.game_channel_id));
                console.log('选择的渠道: ' + _this.detail.game_channel_id);
                _this.detail.channelcg = selectedChannel ? selectedChannel.id : '';
            });
        }

        // 使用 lay-filter 监听渠道ID变化
        form.on('select(game_channel_id)', function(data) {
            const selectedChannel = _this.channels.find(item => item.id === Number(data.value));
            _this.detail.channelcg = selectedChannel ? selectedChannel.id : ''; // 更新 channelcg
            console.log('当前选择的渠道ID: ' + _this.detail.game_channel_id);
            console.log('当前选择的渠道: ' + _this.detail.channel);
        });
             

                form.on('select(type)',function(data){
                    _this.detail.type = data.value;
                })
                form.on('select(configtype)',function(data){
                    _this.detail.configtype = data.value;
                })
                //监听提交
                form.on('submit(submit)', function (data) {
                    var formData = _this.detail;
                    if (Id){ //eidt
                        formData._method = 'put';
                        $.post('/admin/gamePackage/'+Id,formData,function(res){
                            if (res.code===0){
                                layer.msg('success',{icon:6,time:500},function(){
                                    parent.location.reload();//父页面刷新
                                })
                            } else {
                                layer.msg(res.msg,{icon:5})
                            }
                        })
                    } else { //add
                        $.post('/admin/gamePackage/add',formData,function(res){
                            if (res.code===0){
                                layer.msg('success',{icon:6,time:500},function(){
                                    parent.location.reload();//父页面刷新
                                })
                            } else {
                                layer.msg(res.msg,{icon:5})
                            }
                        })
                    }

                    return false;
                });
                form.on("radio(is_test)",function (data) {
                    _this.detail.is_test = data.value
                })
            });
        }
    })
</script>

</body>
</html>